package tv.youxing.android.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import tv.youxing.android.R;
import tv.youxing.android.custom.BaseViewHolder;
import tv.youxing.android.entity.ImageArrayModel;
import tv.youxing.android.ui.FriendVideoPlayerActivity;
import tv.youxing.android.ui.MaxImgDetailsActivity;
import tv.youxing.android.utils.ImageLoader;
import tv.youxing.android.utils.PicassoUtils;

/**
 * Created by apple on 2017/4/20.
 */

/**
 * 项目名：   youxing
 * 包名：     tv.youxing.android .adapter
 * 文件名：   ImageAdapter
 * 创建者：   叶克武
 * 创建时间： 2017/4/20 下午2:49
 * 描述：    TODO
 */
public class ImageAdapter extends BaseAdapter {

    private Activity mActivity;
    private Context context;
    private LayoutInflater layoutInflater;
    private List<ImageArrayModel> ImgUrlDatas = new ArrayList<>();
    public ImageLoader imageLoader;
    public int resources_type=1;
    public String videoUrl;


    public ImageAdapter(Activity activity, Context context, List<ImageArrayModel> urlList) {

        this.mActivity = activity;
        this.context = context;
        this.layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
        this.ImgUrlDatas = urlList;
    }

    public ImageAdapter(Activity activity, Context context, List<ImageArrayModel> urlList,int type,String videoURL) {

        this.mActivity = activity;
        this.context = context;
        this.layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
        this.ImgUrlDatas = urlList;
        this.resources_type = type;
        this.videoUrl = videoURL;
    }



    @Override
    public int getCount() {
        return ImgUrlDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return ImgUrlDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
        {
            vi = layoutInflater.inflate(R.layout.layout_item_image, null);
            final ImageView image= BaseViewHolder.get(vi, R.id.ivImage);
            final ImageView iv_play= BaseViewHolder.get(vi, R.id.icon_play);
//            imageLoader.DisplayImage(ImgUrlDatas.get(position).getThumbnail(), image);
            PicassoUtils.loadImageView(context,ImgUrlDatas.get(position).getThumbnail(), image);

            if (resources_type == 1) {
                iv_play.setVisibility(View.GONE);
            } else if (resources_type == 2) {
                iv_play.setVisibility(View.VISIBLE);
            }


            image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {


                    if (resources_type == 1) {
                        String imgArray[] = new String[ImgUrlDatas.size()];
                        for (int k = 0; k < ImgUrlDatas.size(); k++) {
                            imgArray[k] = ImgUrlDatas.get(k).getImage_url();
                        }
                        Intent intent = new Intent();
                        intent.putExtra("filePath", imgArray);
                        intent.putExtra("PicIndex", (int) position);
                        intent.setClass(context, MaxImgDetailsActivity.class);
                        context.startActivity(intent);
                    } else {
                        Intent intent = new Intent(context, FriendVideoPlayerActivity.class);
                        Bundle bundle = new Bundle();
                        intent.putExtra("videourl",videoUrl);
                        context.startActivity(intent);
                    }


//                    Intent intent = new Intent(mActivity, SpaceImageDetailActivity.class);
////                    intent.putExtra("images", (ArrayList<String>) urlList);
//                    intent.putExtra("imgurl", ImgUrlDatas.get(position).getImage_url());
//                    intent.putExtra("position", position);
//                    int[] location = new int[2];
//                    image.getLocationOnScreen(location);
//                    intent.putExtra("locationX", location[0]);
//                    intent.putExtra("locationY", location[1]);
//
//                    intent.putExtra("width", image.getWidth());
//                    intent.putExtra("height", image.getHeight());
//                    mActivity.startActivity(intent);
//                    mActivity.overridePendingTransition(0, 0);
                }
            });

        }



//        if (viewHolder==null){
//            viewHolder = new ViewHolder();
//            convertView = layoutInflater.inflate(R.layout.layout_item_image, null);
//            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.ivImage);
//            convertView.setTag(viewHolder);
//        }else {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
//        convertView.setBackgroundColor(Color.BLUE);
//        PicassoUtils.loadImageViewHolder(context, urlList.get(position),R.drawable.logo,R.drawable.logo,viewHolder.imageView);


//        imageLoader.DisplayImage(urlList.get(position), viewHolder.imageView);


//        PicassoUtils.loadImageViewSize(context, urlList.get(position),90,90,R.drawable.logo,viewHolder.imageView);

//        final ViewHolder finalViewHolder = viewHolder;
//        viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent(mActivity, SpaceImageDetailActivity.class);
//                intent.putExtra("images", (ArrayList<String>) urlList);
//                intent.putExtra("position", position);
//                int[] location = new int[2];
//                finalViewHolder.imageView.getLocationOnScreen(location);
//                intent.putExtra("locationX", location[0]);
//                intent.putExtra("locationY", location[1]);
//
//                intent.putExtra("width", finalViewHolder.imageView.getWidth());
//                intent.putExtra("height", finalViewHolder.imageView.getHeight());
//                mActivity.startActivity(intent);
//                mActivity.overridePendingTransition(0, 0);
//
//            }
//        });

        return vi;
    }


    class ViewHolder{
        private ImageView imageView;
        private ImageView iv_play;
    }


}
